package com.shujia.async

import java.util.function.Supplier

import com.shujia.bean.{Car, CarWide}
import com.shujia.util.JDBCUtil


class MySupplier(car: Car) extends Supplier[CarWide] {
  //在后台启动一个线程去执行get方法
  override def get(): CarWide = {


    val con = JDBCUtil.getConnection

    val carWide = new CarWide()
    carWide.setCarId(car.getCarId)
    carWide.setSpeed(car.getSpeed)
    carWide.setTs(car.getTs)
    carWide.setKcId(car.getKcId)

    //通过卡口编号查询卡口信息
    val kcId = car.getKcId
    val sql = "select * from kc where id=?"
    val stat = con.prepareStatement(sql)
    stat.setString(1, kcId)
    val result = stat.executeQuery()

    if (result.next()) {
      val dlId = result.getString("dl_id")
      val quId = result.getString("qu_id")
      val rateLimiting = result.getString("rate_limiting")

      carWide.setRateLimiting(rateLimiting.toLong)

      //关联道路表区域表
      val dlstat = con.prepareStatement("select * from dl where id=?")
      dlstat.setString(1, dlId)
      val dlresult = dlstat.executeQuery()
      if (dlresult.next()) {
        val dlName = dlresult.getString("name")
        carWide.setDlName(dlName)
      }

      val quStat = con.prepareStatement("select * from qu where id=?")
      quStat.setString(1, quId)

      val quResult = quStat.executeQuery()
      if (quResult.next()) {
        val city = quResult.getString("city")
        val county = quResult.getString("county")

        carWide.setCity(city)
        carWide.setCounty(county)
      }
    }

    //回收到连接池
    con.close()


    //返回结果
    carWide

  }
}
